

const request = require('../../../utils/request');
const { watch, computed }  = require('../../../utils/computed')

Page({
    data: {
        list:[0,1,2,3,4,5,6,7,8,9],
        activeIndex: 0,
        goods: {},
        foods: [],
        discuss: {},
        reguFoods: '',
        discPrice: 0,
        hiddenRegu: true,
        commitNum: 0,
        localFoods: '',
        foodCart: '', //用作标志
        designdish: '',  //传过来的食物
        designdished: '',
        clearFlag: false,
        goodsDetail: '',
        detailFlag: false,
        type: '外卖',    //外卖订单类型
        imgUrl:request.imgUrl,
        shopId: 0,  //商铺ID

        reguName:[],    //多规格识别名字
        reguSpec: [],   //获取的多规格
        reguPrice: 0    //选择的多规格价格
    },
    onLoad: function(e){
        this.data.shopId = e.id;
        wx.setStorageSync('shopId',e.id)
        let lcfoods = wx.getStorageSync('foods');
        console.log(lcfoods)
        let localFood = [];
        request.HttpRequst(true,'program/shops',false,'',{"id": e.id },"GET",false,res => {
            console.log(res)
            this.setData({
                goods: res.data,
                designdished: res.data.designdish
            })

            //先判断是否存在本地缓存数据
            if(lcfoods != '' || lcfoods.length>0){
                console.log(res.data.designdish)
                res.data.designdish.forEach(val => {
                    if(val.dish == undefined){
                        val.dish = []
                    }else{
                        val.dish.forEach((res,index) => {
                            lcfoods.forEach(food => {
                                if(this.data.shopId == food.shop_id && res.id == food.id){
                                    res = food
                                }
                            })
                            val.dish[index] = res
                        })
                    }
                    
                });

                lcfoods.forEach(food => {
                    if(this.data.shopId == food.shop_id){
                        localFood.push(food)
                    }
                })
                console.log(localFood)

            }
    
            this.setData({
                designdish: res.data.designdish,
                localFoods: localFood,
                foods: localFood
            })
            console.log('localFoods',this.data.localFoods)
            console.log(this.data.shopId)
        })

        //页面转发
        wx.showShareMenu({
            withShareTicket: true
        })
    },

    onReady: function(){
        wx.setNavigationBarTitle({
            title: '商家店铺'
        })    
    },
    bindChange(e){
        var current = e.detail.current;
        this.setData({
            activeIndex: current
        })
    },
    tabClick(e){
        console.log(this.data.Localfoods)
        this.setData({
            activeIndex: e.target.dataset.id
        })
    },
    //计算订单食物
    foodMess(e) {
        console.log(e.detail);
        let food = e.detail.allFoods? e.detail.allFoods.food : e.detail.food;
        let flag = true;
        if(this.data.foods == ''){
            this.data.foods.push(food);
        }else{
            for(let i=0;i<this.data.foods.length;i++){
                if(this.data.foods[i].id == food.id){
                    this.data.foods[i] = food
                    flag = false
                }
            }
            if(flag){
                this.data.foods.push(food);
            }
        }
        for(let i=0;i<this.data.foods.length;i++){
            if(this.data.foods[i].count == 0){
                this.data.foods.splice(i,1)
            }
        }
        console.log('food',food)
        console.log('foods',this.data.foods)
        
        this.setData({
            foods: this.data.foods,
            //orderFoods: this.data.orderFoods
        })
        
        
        
    },
    //多规格菜式数量增加删减
    reguHandle(e) {
        let food = e.detail.allFoods? e.detail.allFoods.food : e.detail.food;
        let flag = true;
        let discPrice =0;
        let orderFlag = true;
        if(this.data.foods == ''){
            this.data.foods.push(food);
        }else{
            for(let i=0;i<this.data.foods.length;i++){
                if(this.data.foods[i].id == food.id){
                    this.data.foods[i] = food
                    flag = false
                }
            }
            if(flag){
                this.data.foods.push(food);
            }
        }
        for(let i=0;i<this.data.foods.length;i++){
            if(this.data.foods[i].count == 0){
                this.data.foods.splice(i,1)
            }
        }

        if(this.data.foods.length == 0){
            this.setData({
                foods: this.data.foods,
                discPrice: 0
            })
        }else{
            for(let i=0;i<this.data.foods.length;i++){
                //this.data.foods[0].price = Math.floor((this.data.foods[0].price + this.data.price0 + this.data.price1)*100)/100
                this.setData({
                    foods: this.data.foods,
                    discPrice: Math.floor((this.data.foods[i].discount * this.data.foods[i].price + this.data.price0 + this.data.price1)*this.data.foods[i].count*100)/100
                })
            }            
        }
        
        console.log(this.data.foods)
    },

    //点击多规格
    handleRegu(e) {
        //console.log(e.detail);
        this.setData({
            hiddenRegu: false,
            reguFoods: e.detail,
            discPrice: 0
        })
    },

    //选择多规格
    selectRegu(e){
        console.log(e)
        let spec = e.currentTarget.dataset.item;
        let I = e.currentTarget.dataset.index;
        let price = 0;
        let flag = true,specFlag = true;
        this.data.reguSpec[I] = spec;
        this.data.reguName[I] = spec.spec_name;
        console.log(this.data.reguSpec,this.data.reguFoods)
        this.data.reguSpec.forEach( res => {
        price += Number(res.spec_price);
        })
        this.data.reguPrice = Number(this.data.reguFoods['discount_price']) + Number(price);
        this.data.reguFoods['spec'] = this.data.reguSpec;
        this.data.reguFoods['count'] = 1;
        console.log(this.data.reguFoods,this.data.reguPrice)
        this.setData({
            reguSpec: this.data.reguSpec,
            reguPrice: this.data.reguPrice,
            reguName: this.data.reguName,
            reguFoods: this.data.reguFoods
        })

        

        //添加到本地存储

    },

    //多规格添加到购物车
    addCart() {
        let flag = true , specFlag = true;
        let local = wx.getStorageSync('foods') || [];
        //debugger

        //添加菜式
        if(this.data.foods == ''){
            this.data.foods.push(this.data.reguFoods);
        }else{
            this.data.foods.forEach( res => {
                if(res.id == this.data.reguFoods.id){
                    res.spec.forEach( (val1,i) => {
                        this.data.reguFoods['spec'].forEach( (val2,j) => {
                            if( i==j){
                                if(val1.spec_name != val2.spec_name){
                                    specFlag = false;
                                }
                            }
                        })
                    })
                    if(specFlag){
                        res.count++;
                        flag = false;
                    }
                }
            })
            if(flag){
                this.data.foods.push(this.data.reguFoods);
            }
        }

        console.log(this.data.reguFoods);
        if(local == ''){
            local.push(this.data.reguFoods);
            
            local.forEach( (res,i) =>{
                if(res.spec){
                    let price2 = 0;
                    res.spec.forEach( val => {
                        price2 += Number(val.spec_price)
                    })
                    local[i].specPrice = price2
                }
            })
            wx.setStorageSync('foods',local)
        }else{
            local.forEach( res => {
                if(res.id == this.data.reguFoods.id){
                    res.spec.forEach( (val1,i) => {
                        this.data.reguFoods.spec.forEach( (val2,j) => {
                            if( i==j){
                                if(val1.spec_name == val2.spec_name){
                                    specFlag = false;
                                }
                            }
                        })
                    })
                    if(specFlag){
                        res.count++;
                        flag = false;
                    }
                }
            })
            if(flag){
                local.push(this.data.reguFoods);
                
                local.forEach( (res,i) =>{
                    if(res.spec){
                        let price1 = 0;
                        res.spec.forEach( val => {
                            price1 += Number(val.spec_price)
                        })
                        local[i].specPrice = price1
                    }
                })
                wx.setStorageSync('foods',local)
            }
        }
        
        this.setData({
            reguFoods: [],
            foods: local,
            hiddenRegu: true,
            reguName:[],
            reguSpec:[]
        })
        console.log(local,this.data.foods)
    },


    hiddenRegu() {
        this.setData({
            hiddenRegu: true,
            reguName:[],
            reguSpec:[]
        })
    },
    getCommitNum(e) {
        this.setData({
            commitNum: e.detail.length  || 0
        })
    },

    //购物车传来的参数
    foodCart(e) {
        console.log(e.detail.allFoods)
        this.setData({
            foodCart: e.detail.allFoods,
            foods: e.detail.foods   //当购物车里面的数据变化时，这个也要跟着改变
        })
        if(wx.getStorageSync('foods')){
            this.data.designdish.forEach(val => {
                val.dish.forEach((res,index) => {
                    if(res.id ==  this.data.foodCart.food.id){
                        res = this.data.foodCart.food
                    }
                    val.dish[index] = res
                })
            });
        }else{
            return false
        }
        this.setData({
            designdish: this.data.designdish    //购物车与菜品联动
        })
    },

    //清空购物车
    clearCart(e){
        this.setData({
            clearFlag: !this.data.clearFlag
        })
    },

    //查看商品详细
    goodsDetail(e){
        this.setData({
            goodsDetail: e.detail,
            detailFlag: true
        })
        console.log(this.data.goodsDetail)
    }
})